<template>
  <div class="home-container">
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        slot="title"
        round
        size="small"
        icon="search"
        class="search-btn"
        type="info"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- 滑动的标签页 -->
    <van-tabs v-model="active" animated swipeable>
      <van-tab :title="item.name" v-for="item in channels" :key="item.id">
        <!-- 文章列表组件 -->
        <articleList :channel="item"/>
      </van-tab>
      <div class="placeholder" slot="nav-right"></div>
      <div class="hamburger-btn" slot="nav-right" @click="show = true">
        <i class="iconfont icon-gengduo"></i>
      </div>
    </van-tabs>

    <!-- 频道列表 -->
    <van-popup
      v-model="show"
      position="bottom"
      closeable
      close-icon-position="top-left"
      :style="{ height: '100%' }"
    >
      <channelEdit
        :myChannels="channels"
        :active="active"
        @updateActive="onUpdateActive"
      ></channelEdit>
    </van-popup>
    <!-- 频道编辑弹出层 -->
  </div>
</template>

<script>
import { getUserChannels } from "@/api/login.js";
import articleList from "@/views/home/component/articleList.vue";
import channelEdit from "@/views/home/component/channelEdit.vue";
import { mapState } from "vuex";
import { getItem } from "@/utils/storage";

export default {
  components: {
    articleList,
    channelEdit,
  },
  computed: {
    ...mapState(["user"]),
  },
  data() {
    return {
      active: 0, //控制被激活的标签项,其实就是索引
      channels: [], //频道列表
      show: false,
    };
  },
  created() {
    this.loadChannels();
  },
  methods: {
    async loadChannels() {
      try {
        let channels = [];
        if (this.user) {
          // 已经登录,判断是否有本地的频道列表数据
          const { data } = await getUserChannels();
          channels = data.data.channels;
        } else {
          // 未登录,判断是否有本地的频道列表数据
          const localChannels = getItem('TOUTIAO_CHANNELS');
          // 有,拿来用
          if(localChannels){
            channels = localChannels;
          } else {
            // 没有,请求获取默认频道列表
            const { data } = await getUserChannels();
            channels = data.data.channels;
          }
        }
        this.channels = channels;
      } catch (err) {
        this.$toast("获取用户频道失败");
      }
    },
    onUpdateActive(index, show = true) {
      // console.log(index)
      this.active = index;
      this.show = show;
    },
  },
};
</script>

<style scoped lang="less">
.home-container {
  padding: 180px 0px 100px 0px;
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 30px;
      color: #fff;
    }
  }
  /deep/.van-nav-bar__title {
    max-width: unset !important;
  }
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }
  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;
    i.iconfont {
      font-size: 33px;
    }
    &:before {
      content: "";
      position: absolute;
      left: 0;
      width: 1px;
      height: 100%;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
  /deep/.van-tabs__wrap {
    position: fixed;
    top: 90px;
    left: 0px;
    right: 0px;
    z-index: 1;
  }
}
</style>